package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Supplier;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISupplierService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @RequestMapping("")
    public String view(){
        return "supplier";
    }
    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(@ModelAttribute("qo")QueryObject qo){

       PageResult result = supplierService.queryForList(qo);

        return result;
    }
    @RequestMapping("/getSelectList")
    @ResponseBody
    public List<Supplier> getSelectList(@ModelAttribute("qo")QueryObject qo){

       List<Supplier> list = supplierService.list();

        return list;
    }
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Supplier supplier){

        JsonResult result = new JsonResult();
        try {
            supplierService.save(supplier);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
       List<Supplier> list = supplierService.list();

        return result;
    }
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(Supplier supplier){

        JsonResult result = new JsonResult();
        try {
            supplierService.update(supplier);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
       List<Supplier> list = supplierService.list();

        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){

        JsonResult result = new JsonResult();
        try {
            supplierService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
       List<Supplier> list = supplierService.list();

        return result;
    }



}
